/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import database.DBPegawai;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.PegawaiBean;

/**
 *
 * @author elfatah
 */
public class PegawaiServlet extends HttpServlet {

    private String username,nama, password, bagian, alamat, telepon, gaji, action, id;
    PegawaiBean bean = new PegawaiBean();
    DBPegawai dbobject = new DBPegawai();

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //  processRequest(request, response);

        action = request.getParameter("action");
        id = request.getParameter("id");
        if (action.equals("edit")) {
            try {
                //get userdetails of particular name
                PegawaiBean ubean = dbobject.getPegawaiDetails(id);
                request.setAttribute("updateuser", ubean);
                RequestDispatcher rd = request.getRequestDispatcher("insert_pegawai");
                rd.forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        if (action.equals("delete")) {
            try {
                //delete userdetails of particular name
                dbobject.deletePegawai(id);
//                List list = dbobject.getAlldetails();
//                request.setAttribute("list", list);
                RequestDispatcher rd = request.getRequestDispatcher("view_pegawai");
                rd.forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }
//
//    /**
//     * Handles the HTTP <code>POST</code> method.
//     *
//     * @param request servlet request
//     * @param response servlet response
//     * @throws ServletException if a servlet-specific error occurs
//     * @throws IOException if an I/O error occurs
//     */

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //  processRequest(request, response);
        PrintWriter out = response.getWriter();

        username = request.getParameter("username");
        nama = request.getParameter("nama");
        password = request.getParameter("password");
        bagian = request.getParameter("bagian");
        alamat = request.getParameter("alamat");
        telepon = request.getParameter("telepon");
        gaji = request.getParameter("gaji");
        action = request.getParameter("action");
        id = request.getParameter("id");
        

        if (action.equals("Insert")) {
            try {
                setValuesToBean();
                dbobject.insertPegawai(bean);
out.print("<script>window.alert('Penambahan pegawai sukses');window.location=('insert_pegawai')</script>");

//                RequestDispatcher rd = request.getRequestDispatcher("insert_pegawai.jsp");
//                rd.forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        if (action.equals("Update")) {
            try {
                setValuesToBean();

                //using DBClass object call insertDetails method and pass bean object
                dbobject.UpdatePegawai(bean, id);
              
                out.print("<script>window.alert('Perubahan data anggota sukses');window.location=('view_pegawai')</script>");
   
            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    public void setValuesToBean() {
       bean.setUsername(username);
        bean.setNama(nama);
        bean.setPassword(password);
//        bean.setRole(bagian);
        bean.setIdBagian(bagian);
        bean.setAlamat(alamat);
        bean.setTelp(telepon);
        bean.setGajiPegawai(gaji);

    }
    
    
    
    

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
